﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// <summary>
/// 获取转移价表单插件
/// </summary>
/// <remarks>
/// 功能：1、点击获取转移价时，获取转移成本价。更新到应收单中
/// 创建人：lxl
/// 创建日期：2025-3-10
/// 修改信息（1）：修改人，修改日期，修改内容
/// </remarks>
namespace LM.CB.K3Cloud.Plugin
{
    [HotUpdate, Description("获取转移价表单插件")]
    public class GetZYJ : AbstractDynamicFormPlugIn
    {

        public override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            //获取年份
            int fyear = DateTime.Now.Year;
            int fmonth = DateTime.Now.Month;
            //string fType =Convert.ToString( this.View.OpenParameter.GetCustomParameter("FType"));
            this.Model.SetValue("F_QLTK_year", fyear);
            this.Model.SetValue("F_QLTK_month", fmonth);
        }
        public override void AfterButtonClick(AfterButtonClickEventArgs e)
        {
            base.AfterButtonClick(e);
            //点击获取数据后
            if (e.Key.ToUpper() == "F_QLTK_GETDATE")//获取数据
            {
                string strSql;
                //获取选取的年月
                string fyear = Convert.ToString(this.Model.GetValue("F_QLTK_year"));
                string fmonth = Convert.ToString(this.Model.GetValue("F_QLTK_month"));

                strSql = $@"/*dialect*/ update b set  b.F_QLTK_ZYJ=isnull(c.F_QLTK_PriceEnd,0),
                b.F_QLTK_ZYJAmount=isnull(b.FBASICUNITQTY,0)*isnull(c.F_QLTK_PriceEnd,0) 
                from t_AR_receivable a join t_AR_receivableEntry b on a.fid=b.fid and year(a.FDATE)={fyear}  and month (a.FDATE)={fmonth}
			    join  T_BD_MATERIAL bb on b.FMATERIALID=bb.FMATERIALID
                left join ( select 
                F_QLTK_PriceEnd,a.F_QLTK_ORGID ,c.fnumber fmaterialNumber from QLTK_t_ZJcostSheet a join QLTK_t_ZJcostSheetE b 
				on a.fid=b.fid and year(a.F_QLTK_Date)={fyear} and month(a.F_QLTK_Date)={fmonth}
				join T_BD_MATERIAL c on b.F_QLTK_product=c.FMATERIALID
				) c  on bb.fnumber=c.fmaterialNumber--物料编码一致
				and b.F_RDLV_NewStockOrgId=c.F_QLTK_ORGID--发货组织一致"; 
                int fresult = DBUtils.Execute(this.Context, strSql);
                if (fresult > 0)
                {
                    var result = new FormResult("sucess");
                    this.View.ReturnToParentWindow(result);
                    this.View.ShowMessage("更新成功");
                }
                else { this.View.ShowMessage("更新失败"); }
            }
        }
    }
}
